﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Web.UI;
using System.Web.UI;
using Medianamik.Core.Configuration;
using System.Configuration;
using Medianamik.Core;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web;
using Medianamik.Globalization;
using Medianamik.Core.Lock;

namespace Medianamik.UI.Lock
{
    [ToolboxData("<{0}:LockInfoToolTip runat=server></{0}:LockInfoToolTip>")]
    public class LockInfoToolTip : RadToolTip //WebControl 
    {

        public LockInfoToolTip()
        {
            this.ShowDelay = 0;
            this.HideEvent = ToolTipHideEvent.ManualClose;
            this.Width = Unit.Pixel(300);
            this.Position = ToolTipPosition.Center;
            this.RelativeTo = ToolTipRelativeDisplay.Mouse;
            this.EnableEmbeddedSkins = false;
            this.Skin = "Medianamik";
        }

        #region Fields

        private static readonly MedianamikConfigSection _config = (MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK);
        
       // RadToolTip _tooltip;
        Label _messageLabel;
         
        #endregion

        #region Events



        #endregion

        #region Properties
                

        public Guid ObjectId
        {
            get
            {
                return ViewState["ObjectId"] as Guid? ?? Guid.Empty;
            }
            set
            {
                ViewState["ObjectId"] = value;
            }
        }

        private LockInfo _lockInfo;
        public LockInfo LockInfo
        {
            get
            {
                return _lockInfo
                    ?? (_lockInfo = LockManager.GetInfo(ObjectId));
            }
        }

        public int Ultimatum
        {
            get
            {
                return (int?)ViewState["Ultimatum"] ?? (int)(ViewState["Ultimatum"] = _config.UltimatumLock /*Default Value*/);
            }
            set
            {
                ViewState["Ultimatum"] = value;
            }
        }

        #endregion

        #region LockDeleted Event

        private static readonly object _lockDeletedEvent = new object();
        public event EventHandler LockDeleted
        {
            add
            {
                Events.AddHandler(_lockDeletedEvent, value);
            }
            remove
            {
                Events.RemoveHandler(_lockDeletedEvent, value);
            }
        }

        protected void OnLockDeleted(EventArgs e)
        {
            EventHandler handler = Events[_lockDeletedEvent] as EventHandler;
            if (handler != null)
            {
                handler(this, e);
            }

        }

        #endregion

        #region Methods

        protected override void CreateChildControls()
        {
            var container = new  HtmlGenericControl("div");
            this.Controls.Add(container);
            var p1 = new HtmlGenericControl("p");
            container.Controls.Add(p1);

            p1.Controls.Add(_messageLabel = new Label
                {
                    ID= ID + "_lblMessage"
                });          

            var p2 = new HtmlGenericControl("p");
            container.Controls.Add(p2);

            var btnUnlock = new Button()
            {
                ID = ID + "_btnUnlock",
                CssClass = "Btn",
                Text = Translator.Translate("Medianamik_LockManagement", "Unlock")
            };
            p2.Controls.Add(btnUnlock);
            btnUnlock.Click += btnUnlock_Click;
           

        }

        void  btnUnlock_Click(object sender, EventArgs e)
        {
            LockManager.UnlockObject(ObjectId);

            OnLockDeleted(EventArgs.Empty);
        }

        public override void DataBind()
        {
            OnDataBinding(EventArgs.Empty);
            EnsureChildControls();
            DataBindChildren();

            var lockInfo = LockInfo;

            if (lockInfo == null)
                _messageLabel.Text = Translator.Translate("Medianamik_LockManagement",
                                                          "LockInfoTooltipMessage_UnknownLock");
            else
            {
                var min = (int)LockInfo.RemainingTime.TotalMinutes;
                var message = Translator.Translate("Medianamik_LockManagement", "LockInfoTooltipMessage");
                _messageLabel.Text = string.Format(message, LockInfo.User, min < 0 ? 0 : min);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls(); 
        }

        #endregion
        
    }
}
